package com.example.project.service.impl;

import com.example.project.action.ActionUtil;
import com.example.project.base.BaseServiceImpl;
import com.example.project.base.ConstKit;
import com.example.project.bean.PVO;
import com.example.project.common.MatchesKey;
import com.example.project.common.ParamKey;
import com.example.project.entity.FirmVO;
import com.example.project.mapper.FirmMap;
import com.example.project.service.FirmService;
import com.example.project.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FirmServiceImpl extends BaseServiceImpl<FirmVO> implements FirmService {
    @Autowired
    FirmMap firmMap;

    @Override
    public void editFirm(Map paramMap) {
        //判断是否有企业pkid传入
        FunctionUtil.check(StringUtil.isEmpty(paramMap.get(ParamKey.PK_ID)),"企业pkid为空！");
        //检查联系电话格式是否正确
        FunctionUtil.matches(paramMap,ParamKey.CONTACT, MatchesKey.PHONE_MATCHES,"企业联系方式不正确！如：18200001111");
        //检查企业联系电话是否重复
        Integer count = firmMap.count(MapUtil.hashMap(new PVO(ParamKey.CONTACT,paramMap.get(ParamKey.CONTACT))));
        FunctionUtil.check(IntegerUtil.isNotEmpty(count),"该联系电话已注册，请重新输入！");
        if (FunctionUtil.equals(paramMap.get(ParamKey.SCHOOL),1)) {
            //添加一条高校认证申请
        }
        //删除不允许修改的企业参数
        MapUtil.remove(paramMap,ParamKey.SCHOOL,ParamKey.ACCOUNT,ParamKey.PASS_WORD);
        firmMap.editFirm(paramMap);
    }

    @Override
    public FirmVO addFirm(FirmVO vo) {
        //检查企业联系电话是否重复
        Integer count = firmMap.count(MapUtil.hashMap(new PVO(ParamKey.CONTACT,vo.getContact())));
        FunctionUtil.check(IntegerUtil.isNotEmpty(count),"该联系电话已注册，请重新输入！");
        vo.setPassword(MD5Util.generate(StringUtil.isNotEmpty(vo.getPassword())?vo.getPassword():ConstKit.DEFAULT_PWD));
        vo.setAccount(vo.getContact());
        vo.setCreateBy(ActionUtil.getParameter(ParamKey.CREATE_BY));
        firmMap.addFirm(vo);
        return vo;
    }

    @Override
    public List<FirmVO> getFirmList(Map paramMap) {
        return firmMap.getFirmList(paramMap);
    }

    @Override
    public void firmSort(Integer pkid,Integer sort) {
        if (sort.equals(1)) {
            firmMap.up(pkid);
        } else {
            firmMap.down(pkid);
        }
    }
}
